﻿<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="${table.entityName}">
	<resultMap type="${config.packageName}<#if table.packageName?exists>${table.packageName}</#if><#if entity.quotePackage?exists>${entity.quotePackage}</#if>.${table.entityName}" id="result${table.entityName}">
		<id column="${table.primaryProperty.name}" property="${table.primaryProperty.property}" jdbcType="${table.primaryProperty.type?upper_case}" />
        <#list table.propertys as property>
        <result column="${property.name}" property="${property.property}" <#if property.type=="boolean">jdbcType="TINYINT"<#elseif property.type!="datetime" >jdbcType="${property.type?upper_case}"</#if>/>
        </#list>
	</resultMap>

	<sql id="Base_Columns">
         ${table.primaryProperty.name}<#list table.propertys as property>,${property.name}</#list>
	</sql>
    
	<!-- 插入操作 -->
	<insert id="insert" parameterType="${config.packageName}<#if table.packageName?exists>${table.packageName}</#if><#if entity.quotePackage?exists>${entity.quotePackage}</#if>.${table.entityName}"<#if table.primaryProperty.increment?exists && table.primaryProperty.increment==true> useGeneratedKeys="true" keyProperty="${table.primaryProperty.name}"</#if>>
		INSERT INTO ${table.tableName}(
        <#if table.primaryProperty.increment?exists && table.primaryProperty.increment==true><#list table.propertys as property><#if property_index!=0>,</#if>${property.name}</#list><#else>
        <include refid="Base_Columns" />
        </#if>
		)VALUES(
        <#if table.primaryProperty.increment?exists && table.primaryProperty.increment==true>
        <#else>
        #${r'{'}${table.primaryProperty.property},jdbcType=${table.primaryProperty.type?upper_case}},
        </#if>
        <#assign propSize="${table.propertys?size-1}"/>
        <#list table.propertys as property>#${r'{'}${property.property}<#if property.type!="datetime" >,jdbcType=${property.type?upper_case}</#if>}<#if (property_index!=table.propertys?size-1)>,</#if></#list>
		)
	</insert>
    
    <sql id="entityKeyWhere">
        <choose>
            <when test="!@cn.ronguan.common.utils.StringUtils@isEmpty(entityKeyValues)">
                ${table.primaryProperty.name} in
                <foreach item="${table.primaryProperty.property}" index="index" collection="entityKeyValues"
                    open="(" separator="," close=")">
                    #${r'{'}${table.primaryProperty.property}}
                </foreach>
            </when>
            <otherwise>
            ${table.primaryProperty.name} = #${r'{'}${table.primaryProperty.property},jdbcType=${table.primaryProperty.type?upper_case}}
            </otherwise>
        </choose>
    </sql>

	<update id="update" parameterType="${config.packageName}<#if table.packageName?exists>${table.packageName}</#if><#if entity.quotePackage?exists>${entity.quotePackage}</#if>.${table.entityName}">
		update ${table.tableName}
		<trim prefix="SET" suffixOverrides=",">
            <#list table.propertys as property>
				<if test="!@cn.ronguan.common.utils.StringUtils@isEmpty(${property.property})">
					${property.name} = #${r'{'}${property.property}<#if property.type!="datetime" >,jdbcType=${property.type?upper_case}</#if>}<#if (property_index!=table.propertys?size-1)>,</#if>
	            </if>
            </#list>
		</trim>
		<where>
            <include refid="entityKeyWhere" />
		</where>
	</update>

	<!-- 删除操作 -->
	<delete id="delete" parameterType="${config.packageName}<#if table.packageName?exists>${table.packageName}</#if><#if entity.quotePackage?exists>${entity.quotePackage}</#if>.${table.entityName}">
		delete from ${table.tableName}		
		<where>
            <include refid="entityKeyWhere" />
		</where>
	</delete>

	<!-- 通用查询条件 -->
	<sql id="BaseWhere">
        <choose>
            <when test="!@cn.ronguan.common.utils.StringUtils@isEmpty(entityKeyValues)">
            and <if test="!@cn.ronguan.common.utils.StringUtils@isEmpty(${table.primaryProperty.property}Alias)">$${r'{'}${table.primaryProperty.property}Alias}.</if>${table.primaryProperty.property} in
            <foreach item="${table.primaryProperty.property}" index="index" collection="entityKeyValues" open="(" separator="," close=")"> 
                    #${r'{'}${table.primaryProperty.property}}
            </foreach>
            </when>
            <when test="!@cn.ronguan.common.utils.StringUtils@isEmpty(${table.primaryProperty.property})">
                and <if test="!@cn.ronguan.common.utils.StringUtils@isEmpty(${table.primaryProperty.property}Alias)">$${r'{'}${table.primaryProperty.property}Alias}.</if>${table.primaryProperty.property} = #${r'{'}${table.primaryProperty.property},jdbcType=${table.primaryProperty.type?upper_case}}
            </when>
        </choose>
        <#list table.propertys as property>
            <if test="!@cn.ronguan.common.utils.StringUtils@isEmpty(${property.property})">
                and <if test="!@cn.ronguan.common.utils.StringUtils@isEmpty(${property.property}Alias)">$${r'{'}${property.property}Alias}.</if>${property.name} = #${r'{'}${property.property}<#if property.type!="datetime" >,jdbcType=${property.type?upper_case}</#if>}
            </if>
        </#list>
	</sql>

	<!-- 删除指定条件的查询 -->
	<delete id="deleteByParams" parameterType="java.util.Map">
		delete from ${table.tableName}
		<where>
			1=1
			<choose>
				<when test="@cn.ronguan.common.utils.MapUtils@getSize(_parameter) >0">
					<include refid="BaseWhere" />
				</when>
				<otherwise>
					and 1=0
				</otherwise>
			</choose>
		</where>
	</delete>

	<!-- 查询，单实体/List通用 -->
	<select id="query" resultMap="result${table.entityName}" parameterType="java.util.Map">
		SELECT
		<include refid="Base_Columns" />
		FROM ${table.tableName}
		<where>
			1=1
            <if test="@cn.ronguan.common.utils.MapUtils@getSize(_parameter) >0">
			<include refid="BaseWhere" />
            </if>
		</where>
        <!-- 排序语句 -->
        <include refid="SystemCommonMapper.querySortSQL" />
        <!-- 分页语句 -->
        <include refid="SystemCommonMapper.queryLimitSQL" />
	</select>

	<!-- 分页统计查询，获取数据总数，需要分页获取的在list查询ID后添加Count为后缀 -->
	<select id="queryCount" resultType="java.lang.Long" parameterType="java.util.Map">
		SELECT count(${table.primaryProperty.name}) FROM ${table.tableName}
		<where>
			1=1
            <if test="@cn.ronguan.common.utils.MapUtils@getSize(_parameter) >0">
			<include refid="BaseWhere" />
            </if>
		</where>
	</select>
    
    <#if table.tree?exists>
	<!-- 树形结构查询语句 -->
	<select id="queryTree" resultMap="SystemCommonMapper.resultTree"
		parameterType="java.util.Map">
		SELECT a.${table.tree.aid} as aid,a.${table.tree.text} as text,(SELECT count(${table.tree.aid}) from ${table.tableName} where ${table.tree.parentId}=a.${table.tree.aid}) as leaf,a.${table.tree.fullPath} as fullPath,a.${table.tree.fullName} as fullName from ${table.tableName} a
		<where>
			1=1
			<if test="@cn.ronguan.common.utils.MapUtils@getSize(_parameter) >0">
				<include refid="BaseWhere" />
			</if>
		</where>
	</select>
    </#if>
</mapper>
